package com.kaiyidiannao.common.util;

import android.content.Context;
import android.util.Log;
import android.widget.Toast;
import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.kaiyidiannao.data.DataCenter;

/**
 * Created by 大志 on 14-4-2.
 */
public class BdMapLocationUitls {
    private Context context;
    private LocationListenter listenter;

    private void test() {
        //百度定位测试
//   final BdMapLocationUitls locationUitls = new BdMapLocationUitls();
//        locationUitls.startLocation();
//        locationUitls.setListenter(new BdMapLocationUitls.LocationListenter() {
//
//            @Override
//            public void onReceiveLocation(BDLocation bdLocation) {
//                if (bdLocation != null) {
//                    String curAddress = bdLocation.getAddrStr(); // 地理位置
//                    if (curAddress != null && !curAddress.trim().isEmpty()) {
//                        Toast.makeText(DdcListActivity.this, "location:" + curAddress, Toast.LENGTH_LONG).show();
//                        Log.d("baiduLocationTest1", curAddress);
//                        locationUitls.stopLocation();
//                    }
//                }
//            }
//        });
    }


    public BdMapLocationUitls() {
        this.context = DataCenter.context;

        if (this.context != null) {
            initLocation();
        }
    }

    // 百度定位请求的客户端类 baidu LocationClient类必须在主线程中声明。需要Context类型的参数。
    private LocationClient mLocClient;
    private MyLocationListener bdLocationListener;
    private BDLocation currentLocation;

    /**
     * 初始化百度定位的 Location类
     */
    private void initLocation() {
        mLocClient = new LocationClient(context);

        // 设置定位参数,参考API http://developer.baidu.com/map/loc_refer/index.html
        LocationClientOption option = new LocationClientOption();
        option.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy);//设置定位模式
        option.setCoorType("bd09ll");//返回的定位(坐标类型)结果是百度经纬度，默认值gcj02
        option.setScanSpan(10000);//设置发起定位请求的间隔时间为5000ms
        option.setIsNeedAddress(true);//返回的定位结果包含地址信息
        option.setNeedDeviceDirect(true);//返回的定位结果包含手机机头的方向
        mLocClient.setLocOption(option);

        bdLocationListener = new MyLocationListener();
        mLocClient.registerLocationListener(bdLocationListener);
    }


    private class MyLocationListener implements BDLocationListener {

        @Override
        public void onReceiveLocation(BDLocation bdLocation) { // 当接收到地图信息的时候，异步请求
            if (listenter != null) {
                listenter.onReceiveLocation(bdLocation);
            }
            Log.d("BDMapDemo", "onReceiveLocation");
        }

        @Override
        public void onReceivePoi(BDLocation bdLocation) {
            Log.d("BDMapDemo", "onReceivePoi=" + bdLocation.getAltitude());

        }
    }

    /**
     * 请求开启定位
     */
    public void startLocation() {
        currentLocation = null;
        mLocClient.start(); // 开启地图服务
        if (mLocClient != null && mLocClient.isStarted()) {
            mLocClient.requestLocation();
        } else {
            Log.d("LocSDK3", "locClient is null or not started");
        }
    }

    /**
     * 停止定位
     */
    public void stopLocation() {
        if (mLocClient != null && mLocClient.isStarted()) {
            mLocClient.stop();
        }
    }


    public interface LocationListenter {
        void onReceiveLocation(BDLocation bdLocation);
    }

    public LocationListenter getListenter() {
        return listenter;
    }

    public void setListenter(LocationListenter listenter) {
        this.listenter = listenter;
    }
}
